//
//  Album.m
//  UI06_UIScorllView_相册
//
//  Created by LGC on 16/3/16.
//  Copyright © 2016年 Apon. All rights reserved.
//

#pragma mark - 宏定义

#define FRAMEWIDTH frame.size.width
#define FRAMEHEIGHT frame.size.height



#pragma mark - 引入头文件

#import "Album.h"
#import "MyScrollView.h"

#pragma mark - 延展
/**
 * 延展
 * 两个子控件(UIPageControl 和 MyScrollView) 属性.
 * UIScrollViewDelegate协议.
 */
@interface Album () <UIScrollViewDelegate>

@property (nonatomic, retain) UIPageControl *page;

@property (nonatomic, retain) MyScrollView *scroll;

@end


#pragma mark - 实现部分

@implementation Album

#pragma mark - 重写dealloc

- (void)dealloc {
    
    [_page release];
    
    [_scroll release];
    
    [super dealloc];
}

#pragma mark - Creating and Init
/** Album初始化 */
- (instancetype)initWithFrame:(CGRect)frame withImages:(NSArray <UIImage *> *)images {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self configWithFrame:frame images:images];
    }
    return self;
    

}

#pragma mark - config
/** Album配置 */
- (void)configWithFrame:(CGRect)frame images:(NSArray *)images {
    
    [self createScrollWithFrame:frame images:images];
    [self createPageControllWithFrame:frame WithImages:images];

}

#pragma mark - 创建scroll对象.
/** 创建scroll对象. */
- (void)createScrollWithFrame:(CGRect)frame images:(NSArray *)images {
    
    // 创建scoll对象,  注意frame参数原点不是(0, 0), 可用bounds属性代替
    self.scroll = [[MyScrollView alloc]initWithFrame:self.bounds withImages:images];
    
    /** 设置代理人 */
    self.scroll.delegate = self;
    
    // 设置第二张图首页显示
    self.scroll.contentOffset = CGPointMake(self.bounds.size.width, 0);
    
    // 设置缩放比例(最小, 最大)
    self.scroll.minimumZoomScale = 0.5;
    self.scroll.maximumZoomScale = 2;
    
    self.scroll.bounces = NO;
    
    self.scroll.showsHorizontalScrollIndicator = NO;
    [self addSubview:self.scroll];
    
    [_scroll release];

}

#pragma mark - 创建pageControll对象.
/** 创建pageControl对象. */
- (void)createPageControllWithFrame:(CGRect)frame WithImages:(NSArray *)images {
    
    // 创建pageControll对象.
    self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, FRAMEHEIGHT - FRAMEHEIGHT / 15.f, FRAMEWIDTH, FRAMEHEIGHT / 15.f)];
    
    // 页数.
    self.page.numberOfPages = images.count - 2;
    
    // 添加事件
    [self.page addTarget:self action:@selector(handlePage:) forControlEvents:UIControlEventValueChanged];
    

    [self addSubview:self.page];
    
    [_page release];


}

#pragma mark - pageControl事件.
/** pageControl事件 */
- (void)handlePage:(UIPageControl *)page {
    
    
    // scroll滚动(contentOffset)
    [self.scroll setContentOffset:CGPointMake(self.bounds.size.width + self.bounds.size.width * page.currentPage , 0) animated:YES];
   

}


#pragma mark - ScrollViewDelegate 协议方法.
/** scrollViewDidScroll */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
   
    if (scrollView.contentOffset.x < self.bounds.size.width) {
        self.page.currentPage = 4;
    }
        if (scrollView.contentOffset.x > self.bounds.size.width * 5) {
        self.page.currentPage = 0;
    }

}
/** scrollViewWillBeginDragging */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
 

}

/** scrollViewWillBeginDecelerating, 滑动将要开始减速 */
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {


}

/** scrollViewDidEndDecelerating, 滑动结束 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
       if (scrollView.contentOffset.x == 0) {
        scrollView.contentOffset = CGPointMake(self.bounds.size.width * 5, 0);
    }
        if (scrollView.contentOffset.x == self.bounds.size.width * 6) {
        scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
    }
    
    // page跟随scoll滚动而改变.
    self.page.currentPage = (scrollView.contentOffset.x - self.bounds.size.width) / self.bounds.size.width;
    

}


/** 返回需要缩放的视图 */
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
 
    return [scrollView.subviews objectAtIndex:scrollView.contentOffset.x / self.bounds.size.width];
    
}
@end
